package com.wwwc.index.web.servlet;

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import com.wwwc.util.web.*;

public class LeftBarServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    {
	try {
	    response.setContentType("text/html");
	    HttpSession session = request.getSession(true);
	    String user_ip  = request.getRemoteAddr();
	    PrintWriter out = response.getWriter();

	    out.println(MyXml.getComments("Left"));

	    String mid = (String)request.getAttribute("mid");
	    if(mid == null) {
		System.out.println("LeftBarServlet:Error:[mid==null]");
		return;
	    }

	    Hashtable coc = (Hashtable)session.getAttribute("ContralOpenClose");
	    Vector keys   = (Vector)session.getAttribute("SideBarKeys");

	    if(coc == null || keys == null) {
		System.out.println("LeftBarServlet:Error:102");
		return;
	    }

	    String call_path = request.getContextPath()+request.getServletPath();
	    String user_name  = null;
	    String user_age   = null;
	    String user_level = null;
	    String user_fname = null;
	    String user_since = null;
	    String user_edu   = null;
	    String user_zip   = null;
	    String user_sex   = null;

	    Hashtable user_info = (Hashtable)session.getAttribute("user_info");
	    if(user_info == null) {
		System.out.println("LeftBarServlet:Error[103]");
		return;
	    }
	    else {
		user_name  = (String)user_info.get("user_name");
		user_age   = (String)user_info.get("user_age");
		user_level = (String)user_info.get("user_level");
		user_fname = (String)user_info.get("user_fname");
		user_since = (String)user_info.get("user_since");
		user_edu   = (String)user_info.get("user_edu");
		user_zip   = (String)user_info.get("user_zip");
		user_sex   = (String)user_info.get("user_sex");
	    }

	    //out.println(LeftBarBean.getLogo());
	    out.println("<BR>");
	    out.println(LeftBarBean.getSignInOutJoin(user_name, call_path));
	    out.println("<BR>");

	    String pstatus = null;
	    String status = null;
	    String show   = null;
	    
	    String did           = null;
	    String sid           = null;
	    String name          = null;
	    String type          = null;
	    String pid           = null;
	    String mlink         = null;
	    String menu_level    = null;
	    String menu_age      = null;
	    String preview_level = null;
	    String preview_age   = null;
	    String read_level    = null;
	    String read_age      = null;
	    String post_level    = null;
	    String post_age      = null;
	    String edit_level    = null;
	    String remove_level  = null;
	    String delete_level  = null;
	    String admin_level   = null;
	    String managers      = null;
	    String admins        = null;

	    String id = (String)request.getAttribute("id");
	    String csid = null;
	    String cstatus = null;
	    String ctype = null;

	    if(id != null && keys.contains(id)) {
		csid      = (String)coc.get(id+"_sid");
		cstatus   = (String)coc.get(id+"_status");
		ctype     = (String)coc.get(id+"_type");
	    }

	    out.println("<TABLE border=0 >");
	    
	    Enumeration enum = keys.elements();
	    while(enum.hasMoreElements()) {
		did = (String)enum.nextElement();
		pid = (String)coc.get(did+"_pid");
		sid = (String)coc.get(did+"_sid");
		pstatus  = (String)coc.get(did+"_pstatus");
		status = (String)coc.get(did+"_status");
		show   = (String)coc.get(did+"_show");
		name   = (String)coc.get(did+"_name");
		type   = (String)coc.get(did+"_type");

		if(show== null || status==null || name == null || type == null) {
		    System.out.println("LeftBarServlet:Error[106]");
		    return;
		}

		//=========================================
		// directory call back setting
		//=========================================

		if(id != null && ctype != null && ctype.equals("0")) {
		    if(cstatus.equals("open")) {
			if(did.equals(id)) {
			    coc.put(did+"_status", "close");
			    status ="close";
			}
			else {
			    if(pid.equals(csid)) {
				coc.put(did+"_pstatus", "close");
				coc.put(did+"_show", "false");
				show = "false";
			    }
			    else {
				if(pid.startsWith(csid)) {
				    coc.put(did+"_show", "false");
				    show = "false";
				}
			    }
			}
		    }

		    if(cstatus.equals("close")) {
			if(did.equals(id)) {
			    coc.put(did+"_status", "open");
			    status = "open";
			}
			else {
			    if(pid.equals(csid)) {
				coc.put(did+"_pstatus", "open");
				coc.put(did+"_show", "true");
				show = "true";
			    }
			    else {
				if(pid.startsWith(csid)) {
				    if(pstatus.equals("open")) {
					coc.put(did+"_show", "true");
					show ="true";
				    }
				}
			    }
			}
		    }
		    session.setAttribute("ContralOpenClose",coc);
		}

		if(show.equals("true")) {
		    StringBuffer sbf = new StringBuffer("");
		    int position = 0;    
		    for (int i=0; i<sid.length();i++) {
			if(sid.charAt(i)=='-') {
			    position++;
			}
		    }

		    for (int n=0; n<position;n++) {
			sbf.append("<td valign='top'><img src=/images/final.gif></td>");
			//sbf.append("<td><img src=/images/space_10.gif></td>");
		    }
		    
		    if(status.equals("close")) {
			if(type.equals("0")) {
			    out.println("<TR><TD>");
			    out.println("<table>");
			    out.println("<tr>");
			    out.println(sbf);
			    out.println("<td><A href='"+call_path+"?id="+did+"'>");
			    out.println("<img src=/images/folder.gif border=0></A>");
			    out.println("</td>");
			    out.println("<td><A href='"+call_path+"?mid="+did+"' style='text-decoration: none;'>");
			    out.println(name);
			    out.println("</A></td></tr></table>");			
			    out.println("</TD></TR>");
			}
		    }

		    if( status.equals("open")) {
			if(type.equals("0")) {
			    out.println("<TR><TD>");
			    out.println("<table>");
			    out.println("<tr>");
			    out.println(sbf);
			    out.println("<td><A href='"+call_path+"?id="+did+"'>");
			    out.println("<img src=/images/folder_open.gif border=0></A></td>");
			    out.println("<td><A href='"+call_path+"?mid="+did+"' style='text-decoration: none;'>");
			    out.println(name);
			    out.println("</A></td></tr></table>");
			    out.println("</TD></TR>");
			}
		    }

		    if(type.equals("1")) {
			out.println("<TR><TD>");
			out.println("<table>");
			out.println("<tr>");
			out.println(sbf);
			out.println("<td><A href='"+call_path+"?mid="+did+"' style='text-decoration: none;'>");
			out.println(name);
			out.println("</A></td></tr></table>");
			out.println("</TD></TR>");
		    }	

		    if(type.equals("2")) {
			out.println("<TR><TD>");
			out.println("<table>");
			out.println("<tr>");
			out.println(sbf);
			mlink=(String)coc.get(did+"_mlink");
			out.println("<td><A href="+mlink+" style='text-decoration: none' target='_blank'>");
			out.println(name);
			out.println("</A></td></tr></table>");
			out.println("</TD></TR>");
		    } 
		    
		    if(type.equals("3")) {
			out.println("<TR><TD>");
			out.println("<table>");
			out.println("<tr>");
			out.println(sbf);
			mlink=(String)coc.get(did+"_mlink");
			out.println("<td><A href="+mlink+" style='text-decoration: none'>");
			out.println(name);
			out.println("</A></td></tr></table>");
			out.println("</TD></TR>");
		    } 
		}
	    }
	    out.println("</TABLE>");
	    out.println(MyXml.getComments("End of Left"));
	}
	catch (IOException e) {
	    System.out.println("LeftBarServlet:Error:107:"+e);
	    return;
	}
	catch (Exception e) {
	    System.out.println("LeftBarServlet:Error:108:"+e);
	    return;
	}
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    {
	doPost(request, response);
    }
}



